#######
# SPDX license identifier: MPL-2.0
#
# Copyright (C) 2011-2015, BMW AG
#
# This file is part of COVESA Project DLT - Diagnostic Log and Trace.
#
# This Source Code Form is subject to the terms of the
# Mozilla Public License (MPL), v. 2.0.
# If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# For further information see http://www.covesa.org/.
#######

# Run CheckIncludeFiles and CheckFunctionExists
include(TestBigEndian)
TEST_BIG_ENDIAN( DLT_BIGENDIAN )
if( DLT_BIGENDIAN )
	add_definitions( -DBYTE_ORDER=BIG_ENDIAN )
else ( DLT_BIGENDIAN )
	add_definitions( -DBYTE_ORDER=LITTLE_ENDIAN )
endif ( DLT_BIGENDIAN )

INCLUDE(CheckIncludeFiles)

CHECK_INCLUDE_FILES( arpa/inet.h HAVE_ARPAINET_H)
CHECK_INCLUDE_FILES( fcntl.h HAVE_FCNTL_H)
CHECK_INCLUDE_FILES( float.h HAVE_FLOAT_H)
CHECK_INCLUDE_FILES( limits.h HAVE_LIMITS_H)
CHECK_INCLUDE_FILES( netdb.h HAVE_NETDB_H)
CHECK_INCLUDE_FILES( netinet/in.h HAVE_NETINETIN_H)
CHECK_INCLUDE_FILES( stddef.h HAVE_STDDEF_H)
CHECK_INCLUDE_FILES( stdint.h HAVE_STDINT_H)
CHECK_INCLUDE_FILES( stdlib.h HAVE_STDLIB_H)
CHECK_INCLUDE_FILES( string.h HAVE_STRING_H)
CHECK_INCLUDE_FILES( sys/ioctl.h HAVE_SYSIOCTL_H)
CHECK_INCLUDE_FILES( sys/socket.h HAVE_SYSSOCKET_H)
CHECK_INCLUDE_FILES( sys/time.h HAVE_SYSTIME_H)
if( WITH_SYSTEMD_WATCHDOG OR WITH_SYSTEMD )
    set(CMAKE_REQUIRED_INCLUDES "${PROJECT_SOURCE_DIR}/systemd/3rdparty/")
    CHECK_INCLUDE_FILES( sd-daemon.h HAVE_SYSTEMD_H)
endif ( WITH_SYSTEMD_WATCHDOG OR WITH_SYSTEMD )
CHECK_INCLUDE_FILES( unistd.h HAVE_UNISTD_H)
CHECK_INCLUDE_FILES( sys/ipc.h HAVE_SYSIPC_H)
CHECK_INCLUDE_FILES( netdb.h HAVE_NETDB_H)
CHECK_INCLUDE_FILES( ctype.h HAVE_CTYPE_H)
CHECK_INCLUDE_FILES( signal.h HAVE_SIGNAL_H)
CHECK_INCLUDE_FILES( syslog.h HAVE_SYSLOG_H)
CHECK_INCLUDE_FILES( sys/stat.h HAVE_SYSSTAT_H)
CHECK_INCLUDE_FILES( linux/stat.h HAVE_LINUXSTAT_H)
CHECK_INCLUDE_FILES( sys/uio.h HAVE_SYSUIO_H)
CHECK_INCLUDE_FILES( termios.h HAVE_TERMIOS_H)
CHECK_INCLUDE_FILES( unistd.h HAVE_UNISTD_H)
CHECK_INCLUDE_FILES( mqueue.h HAVE_MQUEUE_H)

INCLUDE(CheckFunctionExists)

CHECK_FUNCTION_EXISTS( clock_gettime HAVE_FUNC_CLOCKGETTIME)
CHECK_FUNCTION_EXISTS( floor HAVE_FUNC_FLOOR)
CHECK_FUNCTION_EXISTS( fork HAVE_FUNC_FORK)
CHECK_FUNCTION_EXISTS( gethostbyname HAVE_FUNC_GETHOSTBYNAME)
CHECK_FUNCTION_EXISTS( gettimeofday HAVE_FUNC_GETTIMEBYDAY)
CHECK_FUNCTION_EXISTS( inet_ntoa HAVE_FUNC_INETNTOA)
CHECK_FUNCTION_EXISTS( malloc HAVE_FUNC_MALLOC)
CHECK_FUNCTION_EXISTS( memmove HAVE_FUNC_MEMMOVE)
CHECK_FUNCTION_EXISTS( memset HAVE_FUNC_MEMSET)
CHECK_FUNCTION_EXISTS( mkfifo HAVE_FUNC_MKFIFO)
CHECK_FUNCTION_EXISTS( select HAVE_FUNC_SELECT)
CHECK_FUNCTION_EXISTS( socket HAVE_FUNC_SOCKET)
CHECK_FUNCTION_EXISTS( strchr HAVE_FUNC_STRCHR)
CHECK_FUNCTION_EXISTS( strerror HAVE_FUNC_STRERROR)
CHECK_FUNCTION_EXISTS( strstr HAVE_FUNC_STRSTR)
CHECK_FUNCTION_EXISTS( strtol HAVE_FUNC_STRTOL)

# Message queue
find_library(RT_LIBRARY rt)
if(RT_LIBRARY)
  include(CheckLibraryExists)
  CHECK_LIBRARY_EXISTS( ${RT_LIBRARY} mq_open mqueue.h HAVE_FUNC_MQOPEN)
  CHECK_LIBRARY_EXISTS( ${RT_LIBRARY} mq_close mqueue.h HAVE_FUNC_MQCLOSE)
  CHECK_LIBRARY_EXISTS( ${RT_LIBRARY} mq_unlink mqueue.h HAVE_FUNC_MQUNLINK)
  CHECK_LIBRARY_EXISTS( ${RT_LIBRARY} mq_send mqueue.h HAVE_FUNC_MQSEND)
  CHECK_LIBRARY_EXISTS( ${RT_LIBRARY} mq_receive mqueue.h HAVE_FUNC_MQRECEIVE)
else()
  CHECK_FUNCTION_EXISTS( mq_open HAVE_FUNC_MQOPEN)
  CHECK_FUNCTION_EXISTS( mq_close HAVE_FUNC_MQCLOSE)
  CHECK_FUNCTION_EXISTS( mq_unlink HAVE_FUNC_MQUNLINK)
  CHECK_FUNCTION_EXISTS( mq_send HAVE_FUNC_MQSEND)
  CHECK_FUNCTION_EXISTS( mq_receive HAVE_FUNC_MQRECEIVE)
endif()

if(CMAKE_THREAD_LIBS_INIT)
  CHECK_LIBRARY_EXISTS(${CMAKE_THREAD_LIBS_INIT} pthread_setname_np pthread.h HAVE_FUNC_PTHREAD_SETNAME_NP)
else(CMAKE_THREAD_LIBS_INIT)
  CHECK_FUNCTION_EXISTS(pthread_setname_np HAVE_FUNC_PTHREAD_SETNAME_NP)
endif(CMAKE_THREAD_LIBS_INIT)

if(WITH_CHECK_CONFIG_FILE)
  configure_file(${PROJECT_SOURCE_DIR}/cmake/config.h.cmake ${PROJECT_BINARY_DIR}/include/dlt/config.h)
endif(WITH_CHECK_CONFIG_FILE)

configure_file(${PROJECT_SOURCE_DIR}/cmake/dlt_version.h.cmake ${PROJECT_BINARY_DIR}/include/dlt/dlt_version.h @ONLY)
